package abq2.jic.jic.plugin.operate.schoolNews;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EventObject;

/**
 * 动态表单插件
 */
public class newsWindows extends AbstractFormPlugin implements Plugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        Object primaryKey = this.getView().getFormShowParameter().getCustomParam("newsId");
        if (primaryKey != null) {
            QFilter idFilter = new QFilter("id", QCP.equals,primaryKey);
            DynamicObject news = BusinessDataServiceHelper.loadSingle("abq2_mess",
                    "id," + //内码
                            "abq2_textfield," + //标题
                            "abq2_textfield1," + //发布者
                            "abq2_datefield," + //发布日期
                            "abq2_combofield," + //资讯类型
                            "abq2_textareafield",  //资讯内容
                    new QFilter[]{idFilter});
            this.getModel().setValue("abq2_title",news.getString("abq2_textfield"));
            this.getModel().setValue("abq2_time", new SimpleDateFormat("yyyy-MM-dd").format(news.getDate("abq2_datefield")));
            this.getModel().setValue("abq2_author",news.getString("abq2_textfield1"));
            this.getModel().setValue("abq2_type",news.getString("abq2_combofield"));
            this.getModel().setValue("abq2_text",news.getString("abq2_textareafield"));

            //进行单据列表的过滤，使得只显示关于显示该书籍相关的评论
            BillList billList = this.getControl("abq2_billlistap");
            billList.setFilter(new QFilter("abq2_newid",QCP.equals,primaryKey.toString()));
            billList.setOrderBy("abq2_datefield desc");
            billList.refreshData();
            billList.refresh();
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        String operateKey = ((FormOperate) args.getSource()).getOperateKey();
        if (operateKey.equals("sendnewscommand")) {
            //获取评论信息
            String command = (String) this.getModel().getValue("abq2_command_context");

            if (command == null || command.equals("")){
                this.getView().showTipNotification("请输入评论");
                return;
            }

            //新建一个评论，将评论基础资料实体创建
            DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("abq2_news_command");
            StringBuilder sb1 = new StringBuilder(); //生成一个单据编号
            sb1.append("NEWSCOMMAND-");
            for (int i = 1; i <= 10; i++) {
                int ascii = 48 + (int) (Math.random() * 9);
                char c = (char) ascii;
                sb1.append(c);
            }
            dynamicObject.set("number",sb1);
            dynamicObject.set("status","A");
            dynamicObject.set("enable",1);
            dynamicObject.set("abq2_datefield",new Date());
            dynamicObject.set("creator", RequestContext.get().getCurrUserId());
            //评论者设置为当前用户
            dynamicObject.set("abq2_userfield",RequestContext.get().getCurrUserId());
            Object primaryKey = this.getView().getFormShowParameter().getCustomParam("newsId");
            //绑定评论相应的书籍
            dynamicObject.set("abq2_newid",primaryKey.toString());
            //评论内容的赋值
            dynamicObject.set("abq2_context",command);
            SaveServiceHelper.saveOperate("abq2_news_command",new DynamicObject[]{dynamicObject},null);
            BillList billList = this.getControl("abq2_billlistap");
            billList.setFilter(new QFilter("abq2_newid",QCP.equals,primaryKey.toString()));
            billList.setOrderBy("abq2_datefield desc");
            billList.refreshData();
            billList.refresh();
            //发送评论之后，清空原来的评论的内容
            this.getModel().setValue("abq2_command_context",null);
            this.getView().showSuccessNotification("评论成功");
        }
    }
}